package com.ipfsemc.ipfsemc.controller.v1;

import com.ipfsemc.ipfsemc.model.UserInfo;
import com.ipfsemc.ipfsemc.service.IUserInfoService;
import com.ipfsemc.ipfsemc.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/userInfo")
@Api(value = "UserInfoController", tags = "用户信息")

public class UserInfoController {

    private static final Logger logger = LoggerFactory.getLogger(ApplyController.class);
    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private PhoneCode phoneCode;

    @ApiOperation(value = "个人中心",tags = "查询用户个人信息",httpMethod = "GET")
    @RequestMapping(value = "selectUserInfo",method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户ID",required=true,dataType="String")
    })
    public Response<UserInfo> selectByPrimaryKey(String userId) {
        Response jsonData = new Response();
        try {
            if (userId == null || userId.equals("")) {
                jsonData.setMessage("输入参数有误！");
            }else
            {
                UserInfo userInfo1 = userInfoService.selectByPrimaryKey(userId);
                if (null != userInfo1) {
                    jsonData.setResult(userInfo1);
                    jsonData.setCode(0);
                }
            }
        } catch (Exception ex) {
            logger.error("[userInfo][selectUserInfo]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    /**
     * 发送验证码
     *
     * @return
     */

    @ApiImplicitParams({
            @ApiImplicitParam(name="phone",value="用户手机号",required=true,dataType="String")
    })
    @ApiOperation(value = "发送验证码",httpMethod = "POST")
    @RequestMapping(value = "send", method = RequestMethod.POST)
    public ResponseBase send(String phone) {
        ResponseBase jsonData = new ResponseBase();
        try {
            if (null==phone) {
                jsonData.setMessage("输入参数有误！");
            }else {
                return userInfoService.send(phone);
            }


        } catch (Exception ex) {
            logger.error("[userInfo][send]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name="phone",value="用户手机号",required=true,dataType="String")
    })
    @ApiOperation(value = "web端手机登陆",httpMethod = "POST")
    @RequestMapping(value = "webLogin", method = RequestMethod.POST)
    public Response<UserInfo> webLogin(String phone,String code) {
        Response jsonData = new Response();
        try {
            if (null == phone || null ==code) {
                jsonData.setMessage("参数有错误");
            } else {
                jsonData= phoneCode.sureCode(phone,  code);
                if (jsonData.getCode()==0){
                    UserInfo userInfo = userInfoService.userByPhone(phone);
                    jsonData.setResult(userInfo);
                }
            }


        } catch (Exception ex) {
            logger.error("[userInfo][send]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    /**
     * 确定
     *
     * @param
     * @return
     */
    @ApiOperation(value = "确定验证码是否正确",httpMethod = "POST")
    @RequestMapping(value = "sure", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="phone",value="用户手机号",required=true,dataType="String"),
            @ApiImplicitParam(name="code",value="验证码",required=true,dataType="String")
    })
    public ResponseBase sure(String phone,String code) {
        ResponseBase jsonData = new ResponseBase();
        try {
            if (null == phone || null ==code) {
                jsonData.setMessage("参数有错误");
            } else {
               jsonData= phoneCode.sureCode(phone,  code);
               if (jsonData.getCode()==0){
                   userInfoService.updatePhone(phone,code);
               }
            }
        } catch (Exception ex) {
            logger.error("[userInfo][sure]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }




    @ApiOperation(value = "查询所有用户",httpMethod = "GET")
    @RequestMapping(value = "listUser", method = RequestMethod.GET)
    public RespList<UserInfo> listUser() {
        RespList jsonData = new RespList();
        try {
            List<UserInfo> list = userInfoService.list(null);
            jsonData.setResult(list);
            jsonData.setCode(0);
        } catch (Exception ex) {
            logger.error("[userInfo][listUser]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    //伙伴详情
    @ApiOperation(value = "伙伴详情",httpMethod = "GET")
    @RequestMapping(value = "millItem", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户ID",required=true,dataType="String")
    })
    public Response<UserInfo> millItem(String userId) {
        Response jsonData = new Response();
        try {
            UserInfo userInfo=new UserInfo();
            userInfo.setUserId(userId);
            UserInfo user = userInfoService.millItem(userInfo);
            jsonData.setResult(user);
            jsonData.setCode(0);
        } catch (Exception ex) {
            logger.error("[userInfo][millItem]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    //查看冻结余额、可提现余额
    @ApiOperation(value = "查看冻结余额、可提现余额",httpMethod = "GET")
    @RequestMapping(value = "selectBounty", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户ID",required=true,dataType="String")
    })
    public Response<UserInfo> selectBounty(String userId) {
        Response jsonData = new Response();
        try {
            if (null==userId){

                jsonData.setMessage("参数有误！");
            }else{
                UserInfo userInfo=new UserInfo();
                userInfo.setUserId(userId);
                UserInfo user = userInfoService.selectBounty(userInfo);
                jsonData.setResult(user);
                jsonData.setCode(0);
            }

        } catch (Exception ex) {
            logger.error("[userInfo][selectBounty]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }
        return jsonData;
    }

    //转赠fil币
    @ApiOperation(value = "转赠fil币",httpMethod = "POST")
    @RequestMapping(value = "updateFil", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name="phone",value="用户电话",required=true,dataType="String"),
            @ApiImplicitParam(name="fil",value="转赠得fil数量",required=true,dataType="BigDecimal"),
            @ApiImplicitParam(name="toPhone",value="被转赠人的电话",required=true,dataType="String"),
            @ApiImplicitParam(name="code",value="验证码",required=true,dataType="String")
    })
    public ResponseBase updateFil(String phone, String toPhone, String code, BigDecimal fil) {
        ResponseBase jsonData = new ResponseBase();
        try {
            if (null == phone || null == toPhone||null==fil) {
                jsonData.setMessage("参数有错误");
            } else {
                UserInfo userInfo=new UserInfo();
                userInfo.setPhone(phone);
                userInfo.setToPhone(toPhone);
                userInfo.setFil(fil);
                jsonData=phoneCode.sureCode(phone,code);

                if(null!=jsonData.getCode()&&jsonData.getCode()==0){
                    int x = userInfoService.updateFil(userInfo);
                    if (x > 0) {
                        jsonData.setMessage("操作成功！");
                        jsonData.setCode(0);
                    } else if (x == -1) {
                        jsonData.setMessage("您的fil币不足");
                    } else if (x == -2) {
                        jsonData.setMessage("没有此用户！");
                    }else{
                        jsonData.setMessage("操作失败！请重新操作！");
                    }
                }

            }
        } catch (Exception ex) {
            logger.error("[userInfo][updateFil]", ex);
            jsonData.setMessage("系统错误,请稍后重试");
        }

        return jsonData;
    }

}
